import { getStore } from '@/utils/store'
import { getAreaObj } from '@/utils/util'

export const formConfig = {
	inline: false,
	showBtn: true,
	labelPosition: 'right',
	labelWidth: '160px',
	column: [
		{
			label: '应用图标',
			tip: '',
			prop: 'appLogo',
			type: 'upload',
			showFileList: false,
			limit: 1,
			multiple: false,
			span: 24,
		},
		{
			label: '应用名称',
			prop: 'appName',
			maxlength: '50',
			showWordLimit: true,
			type: 'text',
			span: 24,
			rules: [{ required: true, message: '请输入应用名称' }],
		},
		{
			label: '应用标识',
			prop: 'appIdentify',
			type: 'input',
			span: 24,
			rules: [{ required: true, message: '请输入应用标识' }],
		},
		{
			label: '身份码过期时间',
			prop: 'codeExpire',
			type: 'input',
			isNum: true,
			append: '（秒）',
			span: 24,
		},
		{
			label: '数据过期时间',
			prop: 'currentExpire',
			type: 'input',
			isNum: true,
			append: '（秒）',
			span: 24,
		},
		{
			label: 'SSO方式',
			prop: 'ssoType',
			type: 'select',
			dicData: [
				{
					label: '上行',
					value: 0,
				},
				{
					label: '下行',
					value: 1,
				},
			],
			control: (e, model) => {
				return {
					dockUrl: {
						display: e == 1,
					},
				}
			},
			rules: [{ required: true, message: '请选择SSO方式' }],
			span: 24,
		},
		{
			label: '重定向地址',
			prop: 'redirectUrl',
			maxlength: '255',
			showWordLimit: true,
			type: 'text',
			span: 24,
		},
		{
			label: '下行时地址',
			prop: 'dockUrl',
			maxlength: '255',
			showWordLimit: true,
			type: 'text',
			span: 24,
			display: false,
			rules: [{ required: true, message: '请输入下行时地址' }],
		},
		{
			label: '是否自动创建用户',
			prop: 'autoUserFlag',
			type: 'select',
			dicData: [
				{
					label: '否',
					value: 0,
				},
				{
					label: '是',
					value: 1,
				},
			],
			rules: [{ required: true, message: '请选择是否自动创建用户' }],
			span: 24,
		},
		{
			label: '权限区域',
			prop: 'authAreas',
			propName: 'authAreasName',
			type: 'AplidAreaSelect',
			span: 24,
		},
	],
}

export const detailConfig = {
	inline: false,
	showBtn: true,
	labelPosition: 'right',
	labelWidth: '160px',
	column: [
		{
			label: '应用APPID',
			prop: 'id',
			span: 8,
		},
		...formConfig.column.map((item) => {
			return {
				...item,
				span: 8,
			}
		}),
		{
			label: '应用密钥',
			prop: 'appSecret',
			span: 24,
		},
		{
			label: '私钥',
			prop: 'privateKey',
			span: 24,
		},
		{
			label: '公钥',
			prop: 'pubKey',
			span: 24,
		},
	],
}

export const searchConfig = {
	inline: true,
	showBtn: true,
	labelPosition: 'left',
	column: [
		{
			label: '应用标识',
			prop: 'appIdentify',
			type: 'input',
			span: 8,
		},
		{
			label: '应用名称',
			prop: 'appName',
			type: 'input',
			span: 8,
		},
		{
			label: 'SSO方式',
			prop: 'dsType',
			type: 'select',
			dicData: [
				{
					label: '上行',
					value: 0,
				},
				{
					label: '下行',
					value: 1,
				},
			],
			span: 8,
		},
	],
}

export const tableConfig = {
	stripe: true,
	border: true,
	align: 'center',
	columns: [
		{ label: '应用图标', prop: 'appLogo', align: 'center', custome: 'file' },
		{ label: '应用标识', prop: 'appIdentify' },
		{ label: '应用名称', prop: 'appName' },
		{ label: '身份码过期时间', prop: 'codeExpire' },
		{ label: '数据过期时间', prop: 'currentExpire' },
		{
			label: 'SSO方式',
			prop: 'ssoType',
			custome: 'tag',
			dicData: [
				{
					label: '上行',
					value: 0,
					type: 'success',
				},
				{
					label: '下行',
					value: 1,
				},
			],
		},
		{ label: '创建时间', prop: 'createTime' },
		{ label: '更新时间', prop: 'updateTime' },
		{
			label: '操作',
			prop: 'menu',
			width: 300,
			fixed: 'right',
		},
	],
}

export const api = {
	index: 'view_sysSsoConfig_page',
	add: 'view_sysSsoConfig_add',
	edit: 'view_sysSsoConfig_edit',
	delete: 'view_sysSsoConfig_del',
}
export const auth = {
	index: 'view_sysSsoConfig_page',
	add: 'view_sysSsoConfig_add',
	edit: 'view_sysSsoConfig_edit',
	delete: 'view_sysSsoConfig_del',
	mulitSet: 'mulitSet',
}
export const baseConfig = {
	pageName: 'SSO应用凭证',
	dictcodes: ['ROLE_TIER'],
	rowKey: 'id',
	addLabel: '新增',
	tipLabel: 'appName',
	exportMqTopic: '/ning/export/',
	api,
	auth,
	menuBtn: [
		{
			icon: 'detail',
			label: '详情',
			type: 'primary',
			operate: 'detail',
			auth: auth.index,
			text: true,
			show: true,
		},
		{
			icon: 'editor',
			label: '编辑',
			type: 'primary',
			operate: 'edit',
			auth: auth.edit,
			text: true,
			show: true,
		},
		{
			icon: 'delete',
			label: '删除',
			type: 'danger',
			operate: 'delete',
			auth: auth.delete,
			text: true,
			show: true,
		},
	],
	saveFormate: async (form) => {
		return new Promise((resolve, reject) => {
			const userInfo = getStore('userInfo') || {}
			const authAreasObj: any = getAreaObj(form.authAreas, 'auth')
			const authAreasNameObj: any = getAreaObj(form.authAreasName, 'auth')

			resolve({
				...form,
				authProvince: authAreasObj.authProvinceId,
				authCity: authAreasObj.authCityId,
				authArea: authAreasObj.authAreaId,
				authStreet: authAreasObj.authStreetId,
				authCommunity: authAreasObj.authCommunityId,
				...authAreasNameObj,
				tenantId: userInfo.tenantId,
				authAreas: undefined,
				authAreasName: undefined,
			})
		})
	},
	onLoadFormate: (arr) => {
		return new Promise((resolve, reject) => {
			resolve(
				arr.map((item) => {
					const areaIds = [item.authProvince, item.authCity, item.authArea, item.authStreet, item.authCommunity]
					const names = [item.authProvinceName, item.authCityName, item.authAreaName, item.authStreetName, item.authCommunityName]
						.filter(Boolean)
						.join('/')

					return {
						...item,
						authAreas: areaIds,
						authAreasName: names,
					}
				})
			)
		})
	},
	dialogConfig: {
		width: '75%',
	},
	detailConfig,
	searchConfig,
	formConfig,
	tableConfig,
}
